//////////////////////////////////////
// Fast Container Library           //
// MIT License - Copyright (c) 2012 //
//////////////////////////////////////

#pragma once

#include "vector_set.h"


FC_NAMESPACE_BEGIN


template <class Key, size_t N, class Compare, class Allocator>
class fixed_vector_set : public vector_set<Key, Compare, Allocator>
{
public:
	typedef fixed_vector_set<Key, N, Compare, Allocator> 	this_type;
	typedef vector_set<Key, Compare, Allocator>				base_type;
	typedef typename base_type::allocator_type				allocator_type;
	typedef typename base_type::pointer						pointer;

	using base_type::m_allocator;
	using base_type::m_begin;
	using base_type::m_end;
	using base_type::m_capacity;

	typedef aligned_buffer<N * sizeof(Key), FC_ALIGNOF(Key)> buffer_type;

	explicit fixed_vector_set( const Compare& compare = Compare() )
		: base_type(compare, allocator_type(m_buffer.data))
		{
			init();
		}

	template<class InputIterator>
		fixed_vector_set( InputIterator first, InputIterator last, const Compare& compare = Compare() )
		: base_type(compare, allocator_type(m_buffer.data))
		{
			init();
			base_type::insert( first, last );
		}

	fixed_vector_set( const this_type& x )
		: base_type()
		{
			init();
			base_type::assign( x.begin(), x.end() );
		}

	this_type& operator =( const this_type& x )
	{
		base_type::operator =(x);
		return *this;
	}

	bool has_overflowed() const
	{
		return m_begin != get_buffer();
	}

	void reset()
	{
		if( m_capacity )
		{
			base_type::clear();
			base_type::deallocate();
		}

		init();
	}

	void init()
	{
		m_begin = m_end = (pointer)m_buffer.data;
		m_capacity = m_begin + N;
		m_allocator.set_buffer(m_begin);
	}

	pointer get_buffer() const
	{
		return (pointer)m_buffer.data;
	}

protected:
	buffer_type m_buffer;

};



FC_NAMESPACE_END
